package com.vang

/*
Done - B-1: Bids have the following required fields: amount and date/time of bid (integration test)
Done - B-2: Bids are required to be for a Listing (integration test)
Done - B-3: Bids are required to have a bidder (Customer) (integration test)

really this is a bid requirement?
Done - B-4: @see Listing.groovy - A Listing has a list of Bids for that Listing (integration test) ?
Done - B-5: The Bid amount must be at least .50 higher than the previous Bid for the same item (integration test)

TODO - The Bid amount must be at least .50 higher than the previous Bid for the same item (integration test)
B-6: When a Listing is saved, any new Bids added for the listing must be saved (integration test)
-- listing save better just cascade for one to many, else grails is bunk! wtf, why test basic grails functionality?
 */

class Bid implements Comparable {
    private static final Float BID_INC = 0.5f;
	Date dateTime;
	Float amount;

	static belongsTo = [customer:Customer];
    static hasOne = [listing:Listing];

    static constraints = {
	    dateTime(nullable: false);
        amount(nullable: false);
        listing(nullable: false,
                validator: {theListing,  thisObject ->
                    Long currMaxPrice = (theListing.bids == null || theListing.bids.isEmpty()) ? theListing.startingBidPrice : theListing.bids.max().amount ;
                    currMaxPrice = currMaxPrice.longValue() + Bid.BID_INC;
                    return currMaxPrice <= thisObject.amount;
                }
        );
	    customer(nullable: false);
    }

     public int compareTo(other) {
         return (this.amount - other.amount) > 0 ? 1 : ( (this.amount - other.amount) == 0 ? 0 : -1);
     }
}
